package markers;

import common.ChartDemo;
import common.ChartUtil;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.PeriodAxis;
import org.jfree.chart.axis.PeriodAxisLabelInfo;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.ui.Layer;
import org.jfree.chart.ui.LengthAdjustmentType;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;

import java.awt.*;
import java.text.SimpleDateFormat;

public class MarkerDemo2 extends ChartDemo {

    public MarkerDemo2(String title) {
        super(title);
    }

    @Override
    protected Dataset createDataset() {
        TimeSeriesCollection timeSeriesCollection = new TimeSeriesCollection();
        TimeSeries timeseries = new TimeSeries("Temperature");
        timeseries.add(new Hour(0, 30, 6, 2005), 45.299999999999997D);
        timeseries.add(new Hour(1, 30, 6, 2005), 48.899999999999999D);
        timeseries.add(new Hour(2, 30, 6, 2005), 52.100000000000001D);
        timeseries.add(new Hour(3, 30, 6, 2005), 44.799999999999997D);
        timeseries.add(new Hour(4, 30, 6, 2005), 49.899999999999999D);
        timeseries.add(new Hour(5, 30, 6, 2005), 55.5D);
        timeseries.add(new Hour(6, 30, 6, 2005), 58.200000000000003D);
        timeseries.add(new Hour(7, 30, 6, 2005), 58.100000000000001D);
        timeseries.add(new Hour(8, 30, 6, 2005), 63.700000000000003D);
        timeseries.add(new Hour(9, 30, 6, 2005), 66.299999999999997D);
        timeseries.add(new Hour(10, 30, 6, 2005), 69.799999999999997D);
        timeseries.add(new Hour(11, 30, 6, 2005), 70.099999999999994D);
        timeseries.add(new Hour(12, 30, 6, 2005), 72.400000000000006D);
        timeseries.add(new Hour(13, 30, 6, 2005), 69.700000000000003D);
        timeseries.add(new Hour(14, 30, 6, 2005), 68.599999999999994D);
        timeseries.add(new Hour(15, 30, 6, 2005), 70.900000000000006D);
        timeseries.add(new Hour(16, 30, 6, 2005), 73.400000000000006D);
        timeseries.add(new Hour(17, 30, 6, 2005), 77.5D);
        timeseries.add(new Hour(18, 30, 6, 2005), 82.900000000000006D);
        timeseries.add(new Hour(19, 30, 6, 2005), 62.100000000000001D);
        timeseries.add(new Hour(20, 30, 6, 2005), 37.299999999999997D);
        timeseries.add(new Hour(21, 30, 6, 2005), 40.700000000000003D);
        timeseries.add(new Hour(22, 30, 6, 2005), 44.200000000000003D);
        timeseries.add(new Hour(23, 30, 6, 2005), 49.799999999999997D);
        timeSeriesCollection.addSeries(timeseries);
        return timeSeriesCollection;
    }

    @Override
    protected JFreeChart createChart(Dataset dataset) {
        XYDataset xyDataset = (XYDataset) dataset;
        JFreeChart jFreeChart = ChartFactory.createXYLineChart("Marker Demo 2",
                "X", "Temperature", xyDataset,
                PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jFreeChart.getPlot();
        PeriodAxis periodAxis = new PeriodAxis(null, new Hour(0, 30, 6, 2005),
                new Hour(23, 30, 6, 2005));
        PeriodAxisLabelInfo periodAxisLabelInfos[] = new PeriodAxisLabelInfo[2];
        periodAxisLabelInfos[0] = new PeriodAxisLabelInfo(Hour.class, new SimpleDateFormat("HH"));
        periodAxisLabelInfos[1] = new PeriodAxisLabelInfo(Day.class, new SimpleDateFormat("dd-MM"));
        periodAxis.setLabelInfo(periodAxisLabelInfos);
        xyPlot.setDomainAxis(periodAxis);
        ChartUtils.applyCurrentTheme(jFreeChart);

        xyPlot.setDomainGridlinePaint(Color.lightGray);
        xyPlot.setDomainGridlineStroke(new BasicStroke(1.0F));
        xyPlot.setRangeGridlinePaint(Color.lightGray);
        xyPlot.setRangeGridlineStroke(new BasicStroke(1.0F));
        xyPlot.setRangeTickBandPaint(new Color(240, 240, 240));

        ValueAxis valueAxis = xyPlot.getRangeAxis();
        valueAxis.setRange(0.0D, 100.0D);

        XYItemRenderer xyItemRenderer = xyPlot.getRenderer();
        xyItemRenderer.setSeriesPaint(0, Color.green);
        xyItemRenderer.setSeriesStroke(0, new BasicStroke(2.0F));

        ValueMarker valueMarker = new ValueMarker(80.0D);
        valueMarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        valueMarker.setPaint(Color.red);
        valueMarker.setStroke(new BasicStroke(2.0F));
        valueMarker.setLabel("Temperature Threshold");
        valueMarker.setLabelFont(new Font("SansSerif", 0, 11));
        valueMarker.setLabelPaint(Color.red);
        valueMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        valueMarker.setLabelTextAnchor(TextAnchor.BOTTOM_LEFT);
        xyPlot.addRangeMarker(valueMarker);

        Hour hour = new Hour(18, 30, 6, 2005);
        Hour hour1 = new Hour(20, 30, 6, 2005);
        double d = hour.getFirstMillisecond();
        double d1 = hour1.getFirstMillisecond();
        IntervalMarker intervalMarker = new IntervalMarker(d, d1);
        intervalMarker.setLabelOffsetType(LengthAdjustmentType.EXPAND);
        intervalMarker.setPaint(new Color(150, 150, 255));
        intervalMarker.setLabel("Automatic Cooling");
        intervalMarker.setLabelFont(new Font("SansSerif", 0, 11));
        intervalMarker.setLabelPaint(Color.blue);
        intervalMarker.setLabelAnchor(RectangleAnchor.TOP_LEFT);
        intervalMarker.setLabelTextAnchor(TextAnchor.TOP_RIGHT);
        xyPlot.addDomainMarker(intervalMarker, Layer.BACKGROUND);

        ValueMarker valueMarker1 = new ValueMarker(d, Color.blue, new BasicStroke(2.0F));
        ValueMarker valueMarker2 = new ValueMarker(d1, Color.blue, new BasicStroke(2.0F));
        xyPlot.addDomainMarker(valueMarker1, Layer.BACKGROUND);
        xyPlot.addDomainMarker(valueMarker2, Layer.BACKGROUND);
        return jFreeChart;
    }

    public static void main(String[] args) {
        MarkerDemo2 demo2 = new MarkerDemo2("MarkerDemo2");
        ChartUtil.showChartDemo(demo2);
    }
}
